package com.jiexin.service.impl;

import com.jiexin.common.ConfigDatasource;
import com.jiexin.model.Mail;
import com.jiexin.service.MailService;
import com.jiexin.util.ExchangeMailUtil;
import com.jiexin.util.SMTPMailUtil;
import com.jiexin.util.StringUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service
@Component
public class MailServiceImpl
        implements MailService
{
    private static final Logger log = LoggerFactory.getLogger(MailServiceImpl.class);
    @Autowired
    ConfigDatasource configDatasource;

    public void sendMail(String userId, String mailSubject, String message)
    {
        String emails = this.configDatasource.getConfig("adminEmails");
        List<String> emailsStringList = StringUtil.splitToList(emails, ",");
        List<String> emailsList = new ArrayList(emailsStringList);

        Set<String> emailsSet = (Set)emailsList.stream().collect(Collectors.toSet());
        Mail mail = Mail.builder().subject(mailSubject).message(message).receivers(emailsSet).build();
        ExchangeMailUtil.send(mail, this.configDatasource);
    }
}
